package com.csx.data.orm.jpa.IdGenerator;


import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author: liyuan  liyuan1@sunline.cn
 * @date: 2019/7/10 18:32
 * @description:
 * @version:
 **/

public class GwDateUtil {

    public static final String YEAR_MONTH_DAY_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YEAR_MONTH_DAY_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YEAR_MONTH_DAY_HH_MM2 = "yy-MM-dd HH:mm";
    public static final String yearMonthDayHHMMssSSS = "yyyyMMddHHmmssSSS";
    public static final String yearMonthDayHHMMss = "yyyyMMddHHmmss";
    public static final String YEAR_MONTH = "yyyy-MM";
    public static final String SPLIT_CHAR = "-";
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String YEARMONTHDAY = "yyyyMMdd";
    public static final String MONTH_DAY = "MM-dd";

    /**
     * 时间戳-->date
     *
     * @param timeStamp
     * @return
     * @throws ParseException
     */
    public static Date longTodate(Long timeStamp) throws ParseException {
        return new Date(Long.parseLong(String.valueOf(timeStamp)));
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateStringToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(Date date) throws ParseException {
        long ts = date.getTime();
        return ts;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = Long.parseLong(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToString(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 是否超过截止日期  /  结束时间是否在开始时间之前
     *
     * @param now 当前时间 、开始时间
     * @param end 截止日期 、结束时间
     * @return boolean
     */
    public static boolean isExpired(Date now, Date end) {
        if (now.before(end)) {
            return false;
        }
        return true;
    }

    public static String getCurrentFormatDate(String format) {
        LocalDateTime rightNow = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return formatter.format(rightNow);
    }

    public static Date toDate(String time, String pattern) {
        Assert.notNull(time);
        Assert.notNull(pattern);
        return toCalendar(time, pattern).getTime();
    }

    public static GregorianCalendar toCalendar(String time, String pattern) {
        Assert.notNull(time);
        Assert.notNull(pattern);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;

        try {
            date = format.parse(time);
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar;
        } catch (ParseException var5) {
            throw new RuntimeException("[Pase Exception]: the time string doesn't match for pattern.");
        }
    }

    public static String toString(Date date) {
        Assert.notNull(date);
        return toString(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String toString(Date date, String pattern) {
        Assert.notNull(date);
        Assert.notNull(pattern);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
}
